    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deti.es.ukni.cm.account;

import deti.es.persistence.account.AccountDoesNotExist;
import deti.es.persistence.account.AccountManagerRemote;
import deti.es.persistence.account.WrongPassword;
import deti.es.persistence.bandprofile.BandProfileRemote;
import deti.es.persistence.calendar.CalendarManagerRemote;
import deti.es.persistence.network.SocialNetworkRemote;
import deti.es.ukni.cm.persistence.DAOFactory;
import deti.es.ukni.persistence.entity.Account;
import deti.es.ukni.persistence.entity.Band;
import deti.es.ukni.persistence.entity.Bandelement;
import deti.es.ukni.persistence.entity.Event;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Bean to control the business logic.
 *
 * @author ukni
 */
public class AccountBean {

    /**
     * Get all bands that existe in the system.
     *
     * @return A list with all bands.
     */
    public static List<Band> findAllBands(){

        AccountManagerRemote AMBean = DAOFactory.getAccountManager();

        ArrayList<Band> bands = new ArrayList<Band>();
        List<Account> list =  AMBean.findAllAccounts();

        for( Account acc : list ){
            Band band = acc.getBand();
            if ( band != null )
                bands.add(band);
        }
        
        return bands;
    }

    /**
     * Find a particular band given it's ID.
     *
     * @param IDBand Id of the band to search
     * @return The band if it is found, null otherwise.
     */
    public static Band findBand(Integer IDBand){

        AccountManagerRemote AMBean = DAOFactory.getAccountManager();
        try {
            return AMBean.findBand(IDBand);
        } catch (AccountDoesNotExist ex) {
            
        }
        return null;
    }

    /**
     * Get all elements of a band.
     *
     * @param IDBand The band thar we are querying.
     * @return the elements that belongs to the band.
     */
    public static Set<Bandelement> getBandElements(Integer IDBand){

        Band band = AccountBean.findBand(IDBand);
        if ( band != null ){
            return band.getBandelementCollection();
        }else {
            return null;
        }
    }

    /**
     * Gell all events of a band.
     *
     * @param IDBand The band that we are querying.
     * @return all events the belongs to the band.
     */
    public static Set<Event> getBandEvents(Integer IDBand){

        Band band = AccountBean.findBand(IDBand);
        if ( band != null ){
            return band.getEventCollection();
        }else {
            return null;
        }
    }


    private Account account;

    /**
     * Creates an AccountBean and perform a login.
     *
     * @param username
     * @param passwd
     * @throws deti.es.persistence.account.AccountDoesNotExist
     * @throws deti.es.persistence.account.WrongPassword
     */
    public AccountBean(String username, String passwd) throws AccountDoesNotExist, WrongPassword {
        
        AccountManagerRemote AMBean = DAOFactory.getAccountManager();
        this.account = AMBean.login(username, passwd);

    }

    /**
     * Create a new event.
     *
     * @param eventdate
     * @param location
     * @param latitude
     * @param longitude
     * @throws deti.es.ukni.cm.account.NotABandManager
     */
    public void createEvent(Date eventdate, String location,
                            double latitude, double longitude ) throws NotABandManager{

        Band band = this.account.getBand();
        if ( band == null )
            throw new NotABandManager("You don't have a band");
       
        CalendarManagerRemote CMBean = DAOFactory.getCalendarManager();
        CMBean.createEvent(band, eventdate, location, latitude, longitude);

        AccountManagerRemote AMBean = DAOFactory.getAccountManager();
        this.account = AMBean.findAccount( account.getIdaccount() );

    }

    /**
     * Add a new band element to the band.
     * 
     * @param name
     * @param role
     * @param bio
     * @throws deti.es.ukni.cm.account.NotABandManager
     */
    public void addBandElement( String name, String role, String bio ) throws NotABandManager{
        
        Band band = this.getAccount().getBand();
        if ( band == null )
            throw new NotABandManager("You don't have a band");

        BandProfileRemote BPBean = DAOFactory.getBandProfile();
        BPBean.createElement(band, name, role, bio);

        AccountManagerRemote AMBean = DAOFactory.getAccountManager();
        this.account = AMBean.findAccount( account.getIdaccount() );
    }

    /**
     * Post a new video.
     * 
     * @param name
     * @param desc
     * @param embed
     * @throws deti.es.ukni.cm.account.NotABandManager
     */
    public void postVideo(String name, String desc, String embed ) throws NotABandManager{

        Band band = this.getAccount().getBand();
        if ( band == null )
            throw new NotABandManager("You don't have a band");

        BandProfileRemote BPBean = DAOFactory.getBandProfile();
        BPBean.postVideo(band, name, desc, embed);

        AccountManagerRemote AMBean = DAOFactory.getAccountManager();
        this.account = AMBean.findAccount( account.getIdaccount() );
    }

    /**
     * Check if the account is a band manager.
     *
     * @return true if the account is a band manager, false otherwise.
     */
    public boolean hasBand(){ return (this.account.getBand() != null ) ; }

    public Band getBand (){
        if (this.hasBand() )
            return this.account.getBand();
        else
            return null;
    }

    /**
     * Get all events.
     *
     * @return a list with all events.
     */
    public Set<Event> getEvents(){
        Set<Event> events = account.getBand().getEventCollection();
        return events;
    }


    /**
     * Return a list of the last events that the account
     * is following.
     * 
     * @return
     */
    public List<Event> getFollowLatestEvents(){


        Account acc = this.getAccount();
        ArrayList<Event> list = new ArrayList<Event>();

        Set<Band> follow = acc.getBandCollection();

        for( Band b : follow ){
            Set<Event> listE = b.getEventCollection();
            if ( listE.iterator().hasNext() )
                list.add( listE.iterator().next() );
        }

        return list;
    }

    /**
     * Follow a new band.
     *
     * @param IDBand the band to follow.
     */
    public void followBand(Integer IDBand){
        SocialNetworkRemote SNBean = DAOFactory.getSocialNetwork();
        SNBean.followBand(this.getAccount(), IDBand);
    }

    /**
     * @return the account
     */
    public Account getAccount() {
        AccountManagerRemote AMBean = DAOFactory.getAccountManager();
        return AMBean.findAccount(this.account.getIdaccount());
    }
}
